package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author BaiZY on 2020/9/7.
 * @version 1.0
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    /**
     * 加入购物车
     * @param cartItem
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveCartItem(@RequestBody CartItem cartItem){
        cartService.saveCartItem(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> queryCartList(){
        return ResponseEntity.ok(cartService.queryCartList());
    }

    /**
     * 修改购物车
     */
    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("id")Long skuId, @RequestParam("num")Integer num){
        cartService.updateNum(skuId, num);
        return ResponseEntity.noContent().build();
    }

    /**
     * 从购物车中删除
     */
    @DeleteMapping("/{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId")Long skuId){
        cartService.deleteCart(skuId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 购物车合并
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCartItemList(@RequestBody List<CartItem> cartItemList){
        cartService.addCartItemList(cartItemList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
